/*
 * @Author: silence silence9685@163.com
 * @Date: 2023-07-19 10:02:57
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-07-26 15:59:31
 * @FilePath: \ems_neutral_app\lib\pages\Mine\AccountManager\EditCountryPage.dart
 * @Description: 修改国家
 */
import 'package:ems_neutral_app/base/SilScaffold.dart';
import 'package:ems_neutral_app/pages/Mine/AccountManager/ChooseCountryPage.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:ems_neutral_app/utils/color/ColorUtils.dart';
import 'package:ems_neutral_app/utils/router/ARouter.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';
import 'package:sil_utils_package/sil_utils_package.dart';

import '../../../common/widgets/cubitWidget/SubmitBtn.dart';
import '../Language/LanguageController.dart';
import 'Controller/AccountManagerController.dart';

class EditCountryPage extends StatefulWidget {
  const EditCountryPage({super.key});

  @override
  State<EditCountryPage> createState() => _EditCountryPageState();
}

class _EditCountryPageState extends State<EditCountryPage> {
  AccountManagerController _accountManagerController =
      Get.put(AccountManagerController());

  LanguageController _languageController = LanguageController();
  bool _isPromptEnglish = true;

  String? _countryName;
  String? _countryId;

  @override
  void initState() {
    super.initState();
    if (!SilTools.stringIsEmpty(_accountManagerController.countryName.value)) {
      _countryName = _accountManagerController.countryName.value;
    }
    _countryId = _accountManagerController.countryId.value;
    _isPromptEnglish = _languageController.isPromptEnglish();

  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      body: mainWidget(),
    );
  }

  mainWidget() {
    return Container(
      margin: EdgeInsets.only(top: 50.w),
      child: Column(
        children: [
          titleWidget(),
          inputWidget(),
          submitBtnWidget(),
        ],
      ),
    );
  }

  titleWidget() {
    return SilText(
      LangOf(context)?.selectGJ?.country,
      margin: EdgeInsets.only(top: 50.w),
      fontSize: 19.sp,
      color: ColorUtils.color_font_main,
    );
  }

  inputWidget() {
    return SilEmptyButton(
      useConstrained: true,
      backgroundColor: ColorUtils.color_input_bg,
      margin: EdgeInsets.only(top: 50.w),
      radius: 5.w,
      padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 10.w),
      child: Row(
        children: [
          SilImage(
            'imgs/personal/personal_dq.png',
            height: 28.w,
            width: 28.w,
            margin: EdgeInsets.only(left: 14.w, right: 18.w),
          ),
          Container(
            height: 28.w,
            width: 0.5.w,
            margin: EdgeInsets.only(top: 3.w, bottom: 3.w, right: 18.w),
            color: ColorUtils.hexColor('#333333'),
          ),
          SilText(
            _countryName ?? LangOf(context)?.selectGJ?.countryPhlace,
            color: ColorUtils.color_font_main,
            fontSize: 14.sp,
          ),
        ],
      ),
      onTapFunc: () =>
          ARoute.gotoPage(context, ChooseCountryPage()).then((value) {
        if (value == null) {
          return;
        }

        if (!SilTools.stringIsEmpty(value.cnCountry) && !SilTools.stringIsEmpty(value.enCountry)) {
          _countryName = _isPromptEnglish ? value.enCountry : value.cnCountry;
          _countryId = value.id;
          setState(() {});
        }
      }),
    );
  }

  submitBtnWidget() {
    return Container(
      alignment: Alignment.center,
      child: SubmitBtn(
        margin: EdgeInsets.only(top: 20.w),
        title: LangOf(context)?.selectGJ?.accomplish,
        onTapFunc: changeContryHandler,
      ),
    );
  }

  changeContryHandler() {
    _accountManagerController.requestChangeContry(
        context: context,
        countryId: _countryId,
        countryName: _countryName,
        successHandler: (result) {
          ARoute.pop(context);
        });
  }
}
